package com.oocl.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oocl.pojo.Customer;
import com.oocl.service.CustomerService;
import com.oocl.service.impl.CustomerServiceImpl;


public class ListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setHeader("Accept-type", "text/html");
		CustomerService cSer = new CustomerServiceImpl();
		List<Customer> list =  cSer.getList();
		
		String s ="";
		s= "<html>"
				+ "<head></head>"
				+ "<body>"
				+"<div style='margin-left:35%'>"
				+ "<table>";
		int i = 1;
		for(Customer c:list){
			String picturename = "picture"+i+".jpg";
			String path = this.getServletContext().getRealPath("/image/");
			OutputStream out = new FileOutputStream(path+File.separator+picturename);
			System.out.println(c.getPortrait().length);
			out.write(c.getPortrait());
			out.close();
			i++;
			
					s+= "<tr><td>name</td><td>"+c.getUname()+"</td></tr>"
					+ "<tr><td>salary</td><td>"+c.getSalary()+"</td></tr>";
					if(c.isSex()){
						s+= "<tr><td>sex</td><td>Man</td></tr>";
						
					}else{
						s+= "<tr><td>sex</td><td>Weman</td></tr>";
					}
					s+= "<tr><td>birth</td><td>"+c.getBirth()+"</td></tr>"
					+ "<tr><td>portrait</td><td><img width=70 height=70 src='image/"+picturename+"'/>"
					+ "</td></tr>"
					+"<tr></tr>"
					+"<tr></tr>";
		}
		s+= "</table>"
		+"</div>"
		+ "</body>"
		+ "</html>";
		        response.getWriter().println(s);
	}

}
